import { createRouter, createWebHistory } from "vue-router";
import Cookies from "js-cookie";

// 宿舍楼管理五个路由
export const Dormitory = [
  {
    path: "/main/campusManage",
    name: "• 校区管理 CampusManage",
    component: () =>
      import("../views/Main/CampusManage/CampusManage.vue"),
  },
  {
    path: "/main/buildingManage",
    name: "• 楼号管理 BuildingManage",
    component: () =>
      import("../views/Main/BuildingManage/BuildingManage.vue"),
  },
  {
    path: "/main/floorManage",
    name: "• 楼层管理 FloorManage",
    component: () =>
      import("../views/Main/FloorManage/FloorManage.vue"),
  },
  {
    path: "/main/dormSpecs",
    name: "• 宿舍规格 DormSpecs",
    component: () =>
      import("../views/Main/DormSpecs/DormSpecs.vue"),
  },
  {
    path: "/main/dormManage",
    name: "• 宿舍管理 DormManage",
    component: () =>
      import("../views/Main/DormManage/DormManage.vue"),
  },
]

// 综合管理楼层三个路由

export const MainManage = [
  {
    path: "/main/student",
    name: "• 学生管理 Student management",
    component: () =>
      import("../views/Main/Student management/Student.vue"),
  },

  {
    path: "/main/dormAllocation",
    name: "• 宿舍分配 DormAllocation",
    component: () =>
      import("../views/Main/DormAllocation/DormAllocation.vue"),
  },

  {
    path: "/main/dormitory",
    name: "• 宿舍申请 Dormitory application",
    component: () =>
      import("../views/Main/Dormitory/Dormitory.vue"),
  },

]


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/login",
      name: "登录",
      component: () => import("../views/Login/LoginView.vue"),
    },
    {
      path: "/",
      name: "主页",
      component: () => import("../views/Main/MainView.vue"),
      children: [
        // 解构宿舍楼管理路由
        ...Dormitory,
        // 解构综合管理路由
        ...MainManage,


        {
          path: "/main/home",
          name: "首页",
          component: () => import("../views/Home/HomePage.vue"),
        },

        {
          path: "/main/changePsd",
          name: "• 修改密码 ChangePsd",
          component: () =>
            import("../views/Main/ChangePsd/ChangePsd.vue"),
        },
        {
          path: "/",
          redirect: "/main/home",
        },
      ],
    },
    // 重定向

  ],
});
//路由全局拦截
router.beforeEach((to, from, next) => {
  if (!Cookies.get("token") && to.path !== "/login") {
    next("/login");
  } else {
    next();
  }
});
export default router;
